<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频名称" prop="videoName">
        <el-input
          v-model="queryParams.videoName"
          placeholder="请输入视频名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.publish_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resource:video:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['resource:video:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resource:video:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['resource:video:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-back"
          size="mini"
          @click="handleBack"
          v-hasPermi="['bussiness:dynamic:back']"
        >撤回</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="视频ID" align="center" prop="videoId" />
      <el-table-column label="视频名称" align="left" prop="videoName"  show-overflow-tooltip/>
      <el-table-column label="作者" align="left" prop="author"   show-overflow-tooltip />
      <el-table-column label="桥梁" align="left" prop="bridgeName" />
      <el-table-column label="标签" align="left" prop="labelName" />
<!--      <el-table-column label="视频地址" align="center" prop="videoUrl" />-->
<!--      <el-table-column label="视频描述" align="center" prop="videoDesc" />-->
      <el-table-column label="视频封面" align="center" prop="videoCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.videoCover" :isShowThumbnail="true" :width="30" :height="30"/>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.publish_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="显示顺序" align="center" prop="orderNum" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['resource:video:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resource:video:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频资源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :fullscreen="true" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="视频名称" prop="videoName">
              <el-input v-model="form.videoName" placeholder="请输入视频名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="清单对象" prop="bridgeName">
              <el-select v-model="form.bridgeName" placeholder="请选择清单对象" @click.native="openPojectDialog()"  style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标签" prop="labelId">
              <el-select v-model="form.labelId" placeholder="请选择标签" style="width: 100%">
                <el-option
                  v-for="dict in labelList"
                  :key="dict.labelId"
                  :label="dict.labelName"
                  :value="dict.labelId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author" placeholder="请输入作者" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="视频描述" prop="videoDesc">
              <el-input v-model="form.videoDesc"  type="textarea" :rows="4" placeholder="请输入视频描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="视频封面" prop="videoCover">
            <image-upload v-model="form.videoCover" :limit="1"  :isShowSuggest="true" :imageWidth='imageSuggest' />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="视频文件" prop="videoUrl">
              <file-upload :limit="1" v-model="form.videoUrl"  :fileType="['mp4','avi','mpeg','mov','rmvb','mkv']" :fileSize="3000"  />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('0')">暂 存</el-button>
        <el-button type="primary" @click="submitForm('1')">发 布</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查询桥梁列表 -->
    <el-dialog title="清单选择" :visible.sync="dialogVisible" :fullscreen="true" append-to-body width="700px"
               class="app-container search-table-box aidex-table" v-if="dialogVisible">
      <!-- 搜索栏 -->
      <el-card shadow="never" style="margin-bottom: 12px;" class="search_card" v-show="showSearch">
        <div class="filter-container">
          <div class="search_box">
            <el-form :model="projectQueryParams" ref="queryForm1" v-show="showSearch" label-width="80px">
              <el-row :gutter="16">
                <el-col :md="6">
                  <el-form-item label="桥梁名称" prop="projectName">
                    <el-input v-model="projectQueryParams.bridgeName" placeholder="请输入桥梁名称" clearable
                              @keyup.enter.native="inputChange" />
                  </el-form-item>
                </el-col>
                <el-col :md="6">
                  <el-form-item label="桥梁编号" prop="bridgeNum">
                    <el-input v-model="projectQueryParams.bridgeNum" placeholder="请输入桥梁编号" clearable
                              @keyup.enter.native="inputChange" />
                  </el-form-item>
                </el-col>
                <el-col :md="6">
                </el-col>
                <el-col :md="6" align="right" style="float: right;">
                  <el-form-item class="search_btn_box">
                    <el-button class="filter-item" type="primary" @click="inputChange">搜索</el-button>
                    <el-button class="filter-item" style="margin-left: 8px;" @click="resetProjectQuery">重置</el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </div>
      </el-card>
      <el-table stripe ref="tableRef" @row-click="rowClick" :data="bridgeList" :border="true"
                highlight-current-row style="width: 100%;" :height="getInitTableHeight()">
        <el-table-column label="桥梁编号" align="center" prop="bridgeNum" />
        <el-table-column label="桥梁名称" align="center" prop="bridgeName" width="180" />
        <el-table-column label="省" align="center" prop="province" />
        <el-table-column label="市" align="center" prop="city" />
        <el-table-column label="乡/行政区" align="center" prop="county" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.publish_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <div slot="empty">
          <br>
          <svg-icon icon-class="search-none" style="font-size: 64px;" />
          <p>暂无数据</p>
        </div>
      </el-table>

      <pagination v-show="projectTotal > 0" :total="projectTotal" :page.sync="projectQueryParams.pageNum"
                  :limit.sync="projectQueryParams.pageSize" @pagination="inputChange" />
    </el-dialog>
  </div>
</template>

<script>
import { listVideo, getVideo, delVideo, addVideo, updateVideo,videoBack } from "@/api/resource/video";
import { listLabel, getLabel, delLabel, addLabel, updateLabel } from "@/api/resource/label";
import { listList, getList, delList, addList, updateList, withdrawList, moveUpOrDown, pinToTopOrEnd } from '@/api/bussiness/list'
import { deleteOssFile } from "@/api/menu";

export default {
  name: "Video",
  dicts: ['publish_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      imageSuggest: "高250像素，宽420像素",
      projectTotal:0,
      // 视频资源表格数据
      videoList: [],
      bridgeList: [],
      // 标签表格数据
      labelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible:false,
      projectQueryParams:{
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkListId: null,
        videoName: null,
        author: null,
        labelId: null,
        videoUrl: null,
        videoDesc: null,
        videoCover: null,
        status: null,
        isTop: null,
        isBottom: null,
        orderNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        videoName: [
          { required: true, message: "视频名称不能为空", trigger: "change" }
        ],
        videoUrl: [
          { required: true, message: "视频不能为空，请先上传视频！！！", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getLabelList();
    this.getList();
  },
  methods: {
    async getLabelList() {
      var para = {
        pageNum: 1,
        pageSize: 1000,
      };
      await listLabel(para).then(response => {
        this.labelList = response.rows;
        this.total = response.total;
      });
    },
    /** 查询视频资源列表 */
    getList() {
      this.loading = true;
      listVideo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      //如果是新增，并且视频不为空，删除
      //如果是修改，并且视频为空，更新状态为暂存，视频更新为空。
      if (this.form.videoId != null && (this.form.videoUrl== null || this.form.videoUrl == undefined)) {
        this.form.status = '0';
        this.form.videoUrl = '';
        updateVideo(this.form).then(response => {
          this.open = false;
          this.reset();
          this.getList();
        });
      } else if(this.form.videoId == null && !!this.form.videoUrl) {
        let fileName = this.form.videoUrl.replace(
          "https://bridgebj.oss-cn-beijing.aliyuncs.com/",
          ""
        );
        // console.log(",,,", fileName);
        deleteOssFile({ fileName });
        this.open = false;
        this.reset();
        this.getList();
      }else {
        this.open = false;
        this.reset();
      }
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: null,
        checkListId: null,
        videoName: null,
        author: null,
        labelId: null,
        videoUrl: null,
        videoDesc: null,
        videoCover: null,
        status: null,
        isTop: null,
        isBottom: null,
        orderNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.videoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频资源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const videoId = row.videoId || this.ids
      getVideo(videoId).then(response => {
        this.form = response.data;
        this.form.labelId = parseInt(this.form.labelId);
        this.open = true;
        this.title = "修改视频资源";
      });
    },
    handleBack(row) {
      const dynamicIds = row.videoId || this.ids;
      if (dynamicIds.length == 0) {
        this.$modal.msgError("请先选择视频信息！");
        return;
      }
      this.$modal
        .confirm('是否确认撤回视频编号为"' + dynamicIds + '"的数据项？')
        .then(function () {
          return videoBack(dynamicIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("撤回成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.status = status;
          if (this.form.videoId != null) {
            updateVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoIds = row.videoId || this.ids;
      this.$modal.confirm('是否确认删除视频资源编号为"' + videoIds + '"的数据项？').then(function() {
        return delVideo(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/video/export', {
        ...this.queryParams
      }, `video_${new Date().getTime()}.xlsx`)
    },
    openPojectDialog() {
      this.dialogVisible = true;
      this.bridgeList = [];
      this.inputChange();
    },
    async inputChange() {
      this.projectQueryParams.status = "1";
      listList(this.projectQueryParams).then((response) => {
        this.bridgeList = response.rows
        this.projectTotal = response.total
      })
    },
    resetProjectQuery() {
      this.projectQueryParams = this.$options.data().projectQueryParams
      this.inputChange()
    },
    rowClick(row) {
      if (row) {
        this.form.checkListId = row.checkListId;
        this.form.bridgeName = row.bridgeName;
        this.dialogVisible = false;
      }
    },
  }
};
</script>
